import { Effect } from 'dva';
import { Reducer } from 'redux';
import { produce, Draft } from 'immer';

import {
  logDeviceStatisVo,
  logDeviceList,
  operLogStatisVo,
  operLogPage,
  queryByDeviceType,
  logdeviceAlamList,
  logDeviceStateList,
} from '@/services/journalManage';

// 设备日志统计约束
export interface QueryLogDeviceStatisVo {
  dayCount: number; // 今日统计 ,
  everyCount: number; // 所有统计 ,
  monthCount: number; // 近一月 统计 ,
  weekCount: number; // 近一周 统计
}
//设备日志查询字段
export interface QueryLogDeviceVoList {
  createTime: string; // 发生时间 ,
  deviceId: number; // 设备ID ,
  deviceTypeNo: number; // 设备类型编码 ,
  logData: string; // 返回参数 ,
  logDeviceId: number; // 主键ID ,
  logType: number; // 日志类型(1、设备报警，2、设备状态；3、实时报文) ,
  typeName: string; // 设备名称
}
//平台日志统计字段
export interface QueryOperLogStatisVo {
  dayCount: number; // 今日 统计
  monthCount: number; //  近一月 统计
  weekCount: number; //  近一周 统计
}
//平台日志查询字段
export interface QueryOperLogPage {
  businessType: number; // 业务类型 ,
  deptName: string; // 部门名称 ,
  errorMsg: string; // 错误消息 ,
  methodName: string; // 方法名称 ,
  moduleTitle: string; // 模块标题 ,
  operId: number; // 主键ID ,
  operIp: string; // 主机地址 ,
  operName: string; // 登录账号 ,
  operParam: string; // 请求参数 ,
  operStatus: number; // 操作状态(0正常,1异常) ,
  operTime: string; // 操作时间 ,
  operUrl: string; // 请求URL ,
  operatorType: number; // 操作人类别
}
export interface RoleDeviceVo {
  deviceId: number; // 设备ID ,
  deviceName: string; // 设备名称 ,
  deviceTypeParentNo: number; // 设备类型父级编码 ,
  roleDevicePowerId: number; // 设备权限主键ID ,
  stuts: string; // 勾选状态(1是，0否)
}

//设备报警查询字段
export interface QuerylogdeviceAlamList {
  alamContent: string; //报警内容 ,
  alamImg: string; //报警截图 ,
  alamType: number; // 报警类型 ,
  alamTypeName: string; //报警类型名称 ,
  createId: number; // 创建人 ,
  createTime: string; //创建时间 ,
  deleteFalg: number; // 删除状态(0、未删除，1、已删除) ,
  deviceAlamId: number; // 主键ID ,
  deviceId: number; // 设备ID ,
  happenTime: string; //发生时间 ,
  processState: number; // 处理状态(0、待处理，1、已处理，2、处理失败) ,
  remarks: string; //备注 ,
  updateId: number; // 更新人 ,
  updateTime: string; //更新时间
}
//设备日志状态查询字段
export interface QuerylogDeviceStateList {
  createTime: string; //创建时间 ,
  deviceId: number; // 设备ID ,
  deviceName: string; //设备名称 ,
  deviceState: number; // 设备状态：（1、正常、2、异常） ,
  deviceTypeNo: number; // 设备类型编码(1000，前两位是第一级，后二位是第二级) ,
  logDeviceStateId: number; // 主键ID ,
  stateCode: number; // 状态码 ,
  stateName: string; //状态码名称 ,
  typeName: string; //类型名称
}
export interface JournalManageModelState {
  logDeviceStatisVo: QueryLogDeviceStatisVo;
  logDeviceList: QueryLogDeviceVoList[];
  logdeviceAlamList: QuerylogdeviceAlamList[];
  logDeviceStateList: QuerylogDeviceStateList[];
  operLogStatisVo: QueryOperLogStatisVo;
  operLogPage: QueryOperLogPage[];
  queryByDeviceType: RoleDeviceVo;
}
export interface JournalManageModelType {
  namespace: string;
  state: JournalManageModelState;
  effects: {
    logDeviceStatisVo: Effect;
    operLogStatisVo: Effect;
    queryByDeviceType: Effect;
  };
  reducers: {
    change_logDeviceStatisVo: Reducer<JournalManageModelState>;
    change_logDeviceList: Reducer<JournalManageModelState>;
    change_operLogStatisVo: Reducer<JournalManageModelState>;
    change_operLogPage: Reducer<JournalManageModelState>;
    change_queryByDeviceType: Reducer<JournalManageModelState>;
    change_logdeviceAlamList: Reducer<JournalManageModelState>;
    change_logDeviceStateList: Reducer<JournalManageModelState>;
  };
}

const Model: JournalManageModelType = {
  namespace: 'journalManageMode',

  state: {
    logDeviceStatisVo: {},
    logDeviceList: [],
    operLogStatisVo: {},
    operLogPage: [],
    logdeviceAlamList: [],
    logDeviceStateList: [],
    queryByDeviceType: {},
  },

  effects: {
    *logDeviceStatisVo({ payload }, { call, put }) {
      let [err, data] = yield call(logDeviceStatisVo, payload);
      if (!err) yield put({ type: 'change_logDeviceStatisVo', payload: data });
      [err, data] = yield call(logDeviceList, payload);
      if (!err) yield put({ type: 'change_logDeviceList', payload: data });
    },
    *operLogStatisVo({ payload }, { call, put }) {
      let [err, data] = yield call(operLogStatisVo, payload);
      if (!err) yield put({ type: 'change_operLogStatisVo', payload: data });
      [err, data] = yield call(operLogPage, payload);
      if (!err) yield put({ type: 'change_operLogPage', payload: data });
    },
    *queryByDeviceType({ payload }, { call, put }) {
      let [err, data] = yield call(queryByDeviceType, payload);
      if (!err) yield put({ type: 'change_queryByDeviceType', payload: data });
    },
    *logdeviceAlamList({ payload }, { call, put }) {
      let [err, data] = yield call(logdeviceAlamList, payload);
      if (!err) yield put({ type: 'change_logdeviceAlamList', payload: data });
    },
    *logDeviceStateList({ payload }, { call, put }) {
      let [err, data] = yield call(logDeviceStateList, payload);
      if (!err) yield put({ type: 'change_logDeviceStateList', payload: data });
    },
  },

  reducers: {
    change_logDeviceStatisVo: produce(
      (draft: Draft<JournalManageModelState>, { payload }): void => {
        draft.logDeviceStatisVo = payload;
      },
    ),
    change_logDeviceList: produce((draft: Draft<JournalManageModelState>, { payload }): void => {
      draft.logDeviceList = payload;
    }),
    change_operLogStatisVo: produce((draft: Draft<JournalManageModelState>, { payload }): void => {
      draft.operLogStatisVo = payload;
    }),
    change_operLogPage: produce((draft: Draft<JournalManageModelState>, { payload }): void => {
      draft.operLogPage = payload;
    }),
    change_queryByDeviceType: produce(
      (draft: Draft<JournalManageModelState>, { payload }): void => {
        draft.queryByDeviceType = payload;
      },
    ),
    change_logdeviceAlamList: produce(
      (draft: Draft<JournalManageModelState>, { payload }): void => {
        draft.logdeviceAlamList = payload;
      },
    ),
    change_logDeviceStateList: produce(
      (draft: Draft<JournalManageModelState>, { payload }): void => {
        draft.logDeviceStateList = payload;
      },
    ),
  },
};

export default Model;
